package com.yjt.admin.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.dao.EmpRoleMapper;
import com.yjt.admin.system.entity.EmpRole;
import com.yjt.admin.system.form.EmpRoleForm;
import com.yjt.admin.system.service.EmpRoleService;
import com.yjt.admin.system.vo.EmplyVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 员工角色表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-12
 */
@Service
public class EmpRoleServiceImpl extends ServiceImpl<EmpRoleMapper, EmpRole> implements EmpRoleService {

    @Resource
    private EmpRoleMapper empRoleMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEmpRole(EmpRoleForm empRoleForm) {
        //通过角色id删除所有
        this.remove(Wrappers.<EmpRole>lambdaUpdate().eq(EmpRole::getRoleId, empRoleForm.getRoleId()));

        //构造数据
        List<EmpRole> newList = empRoleForm.getEmplyIdList().stream()
                .map(e -> new EmpRole(null, e, empRoleForm.getRoleId())).collect(Collectors.toList());

        //保存数据
        this.saveBatch(newList);
    }

    @Override
    public List<EmplyVO> getEmpRole(String roleId) {
        return empRoleMapper.findEmplyByRoleId(roleId);
    }
}
